using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LogLayer : MonoBehaviour
{
    [SerializeField]
    RectTransform rectTransform;
    [SerializeField]
    UnityEngine.Events.UnityEvent playMaxDone;
    [SerializeField]
    UnityEngine.Events.UnityEvent playMinDone;
    [SerializeField]
    AnimationCurve playMinCurve, playMaxCurve;

    [SerializeField]
    float deltaTime = 0.1f;
    float start;

    bool isPlaying;
    bool isMin;

    public void PlayMin()
    {
        if (isPlaying)
            return;
        isMin = true;
        isPlaying = true;
        start = Time.time;
    }

    public void PlayMax()
    {
        if (isPlaying)
            return;
        isMin = false;
        isPlaying = true;
        start = Time.time;
    }

    // Update is called once per frame
    void Update()
    {
        if (!isPlaying)
            return;

        var now = Time.time;
        if(start + deltaTime <= now)
        {
            isPlaying = false;
            if (!isMin)
            {
                playMaxDone.Invoke();
                rectTransform.localScale = Vector3.one;
            }
            else
            {
                playMinDone.Invoke();
                rectTransform.localScale = Vector3.zero;
            }
        }
        else
        {
            var t = Mathf.Clamp01((now - start) / deltaTime);
            AnimationCurve curve = isMin ? playMinCurve : playMaxCurve;
            var v = curve.Evaluate(t);
            rectTransform.localScale = new Vector3(v, v, v);
        }
    }
}
